/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConversationView.java
 *
 * Created on Apr 2, 2012, 11:16:18 PM
 */
package org.nightknight.satobee.gui;

import java.util.Date;
import javax.swing.DefaultListModel;
import org.jdesktop.application.Action;
import org.nightknight.satobee.core.Conversation;
import org.nightknight.satobee.core.Message;

/**
 *
 * @author nightknight
 */
public class ConversationView extends javax.swing.JFrame {

    /** Creates new form ConversationView */
    public ConversationView() {
        model = new DefaultListModel();
        
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        messageList = new javax.swing.JList();
        closeButton = new javax.swing.JButton();
        sendButton = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        message = new javax.swing.JTextArea();
        menuBar = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        messageList.setModel(model);
        messageList.setName("messageList"); // NOI18N
        jScrollPane1.setViewportView(messageList);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(org.nightknight.satobee.gui.SatoBeeGUIApp.class).getContext().getActionMap(ConversationView.class, this);
        closeButton.setAction(actionMap.get("quit")); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(org.nightknight.satobee.gui.SatoBeeGUIApp.class).getContext().getResourceMap(ConversationView.class);
        closeButton.setText(resourceMap.getString("closeButton.text")); // NOI18N
        closeButton.setName("closeButton"); // NOI18N

        sendButton.setAction(actionMap.get("sendMessage")); // NOI18N
        sendButton.setText(resourceMap.getString("sendButton.text")); // NOI18N
        sendButton.setName("sendButton"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        message.setColumns(20);
        message.setRows(5);
        message.setName("message"); // NOI18N
        jScrollPane2.setViewportView(message);

        menuBar.setName("menuBar"); // NOI18N

        jMenu1.setText(resourceMap.getString("jMenu1.text")); // NOI18N
        jMenu1.setName("jMenu1"); // NOI18N
        menuBar.add(jMenu1);

        jMenu2.setText(resourceMap.getString("jMenu2.text")); // NOI18N
        jMenu2.setName("jMenu2"); // NOI18N
        menuBar.add(jMenu2);

        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 512, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 512, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(sendButton)
                        .addGap(18, 18, 18)
                        .addComponent(closeButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 247, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(closeButton)
                    .addComponent(sendButton)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
// TODO add your handling code here:
    conv.closeConnection();
}//GEN-LAST:event_formWindowClosing

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ConversationView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ConversationView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ConversationView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ConversationView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ConversationView().setVisible(true);
            }
        });
    }

    @Action
    public void sendMessage() {
        //发送信息
        String text = message.getText();
        
        //将信息添加到显示区域
        Message mes = new Message();
        mes.setReceiver(conv.getContact());
        mes.setSender(Conversation.me);
        mes.setSendTime(new Date());
        mes.setType(1);
        mes.setContent(text);
        
        if (!conv.sendMessage(mes)) {
            mes.setContent("message send failed, probably "
                    + "caused by the user you contact with has quit");
        }
        
        addElement(mes);
    }

    @Action
    public void quit() {
        conv.closeConnection();
        this.dispose();
    }
    
    /**
     * 向列表中加入元素
     * @param message 
     */
    public void addElement(Message message) {
        //输出发送者与发送时间
        model.addElement(message.getSender().getAddresss() + "  " + message.geteSendTime());
        //输出发送内容
        model.addElement("\t" + message.getContent());
    }
    
    public Conversation getConversation() {
        return this.conv;
    }
    
    public void setConversation(Conversation conv) {
        this.conv = conv;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton closeButton;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JTextArea message;
    private javax.swing.JList messageList;
    private javax.swing.JButton sendButton;
    // End of variables declaration//GEN-END:variables

    private DefaultListModel model;
    private Conversation conv;
}
